package com.yanfan.energy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yanfan.attach.mapper.DeviceDetailMapper;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.energy.domain.PurchasingBody;
import com.yanfan.energy.domain.Workshop;
import com.yanfan.energy.domain.statist.DateByStatistics;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.domain.statist.RangeStatistics;
import com.yanfan.energy.mapper.*;
import com.yanfan.energy.service.TotalEnergyService;
import com.yanfan.energy.util.WidgetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author caijinkang
 * @date 2022年09月28日 15:46
 */
@Service("totalEnergyServiceImpl")
@Transactional(rollbackFor = Exception.class)
public class TotalEnergyServiceImpl implements TotalEnergyService {

    public static final String Day = "day";
    public static final String WEEK = "week";
    public static final String MONTH = "month";
    private static final ZoneId zone = ZoneId.systemDefault();
    // true:根据能耗单价计算，false:根据单价计算
    private final static boolean setScheme = true;
    @Resource
    private OverallEnergyDao overallEnergyDao;
    @Resource
    private WorkshopDao workshopDao;
    @Resource
    private EnergyPriceSettingDao energyPriceSettingDao;
    @Autowired
    private DeviceEnergyMapper deviceEnergyMapper;
    @Autowired
    private PurchasingEnergysDao purchasingEnergyDao;

    @Override
    public Map<String, Map<String, List<DateStatistics>>> findTotalCost(Long workshopId, Integer floorId, Integer buildingId, String range, LocalDate begin, LocalDate end, Integer unit) {

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end, "%Y-%m-%d", "'%Y-%m-%d'");

        Map<String, Map<String, List<DateStatistics>>> mapResult = new LinkedHashMap<>();

        List<DateStatistics> work = workshopDao.queryByWorkId(null);

        if (!Objects.isNull(workshopId)) {
            work = workshopDao.queryByWorkId(workshopId);
            buildingId = null;
        }

        // 展示楼栋下的车间
        if (Objects.isNull(workshopId) && Objects.isNull(floorId) && !Objects.isNull(buildingId)) {
            work = workshopDao.findWorkshopByBuildingId(buildingId);
        }
        // 展示楼层下的车间
        if (Objects.isNull(workshopId) && !Objects.isNull(floorId) && !Objects.isNull(buildingId)) {
            work = workshopDao.findWorkshopByFloorId(floorId);
        }

        final String date = rangeStatistics.getDatetime();
        final LocalDate beginOn = rangeStatistics.getBegin();
        final LocalDate endOn = rangeStatistics.getEnd();

        Map<String, List<DateStatistics>> workshopByWe = new LinkedHashMap<>();

        Integer finalBuildingId = buildingId;
        work.stream().forEach(w -> {
            // 用电
            List<DateByStatistics> electricity = new ArrayList<>();
            // 所有能耗
            List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), w
                    .getCount()
                    .intValue(), floorId, finalBuildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 6：蒸汽
            List<DateStatistics> vapour = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 6L)
                    .collect(Collectors.toList());
            // 4：压缩空气
            List<DateStatistics> compress = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 4L)
                    .collect(Collectors.toList());
            // 1：冷却水(空调热能)
            List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
            // 2：软化水(自来水)
            List<DateStatistics> softening = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 2L)
                    .collect(Collectors.toList());
            // 默认单位：能耗
            if (unit == 0) {
                // 用电
                List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), w
                        .getCount()
                        .intValue(), floorId, finalBuildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                electricity = WidgetUtil.mergeByDateBy(daoTotalCost);
            }
            // 能耗价格：万元
            if (unit == 1) {
                List<DateByStatistics> daoTotalCost = new ArrayList<>();

                // 数据量过大时，查询月的去年表
                if ("month".equals(range) && Objects.isNull(w.getCount().intValue())) {
                    daoTotalCost = deviceEnergyMapper.findConsumeElectricityPriceByYear(rangeStatistics.getDatetime(), w
                            .getCount()
                            .intValue(), floorId, finalBuildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                    electricity = WidgetUtil.mergeByDateBy(daoTotalCost);
                } else {
                    // 用电
                    daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), w
                            .getCount()
                            .intValue(), floorId, finalBuildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                    electricity = WidgetUtil.mergeByDateBy(daoTotalCost);
                }

                // 蒸汽
                // 天然气费 天然气费(一个月)÷蒸汽用量(一个月)=单价x蒸汽能耗m3
                // 天然气费用 natural
                List<DateStatistics> natural = purchasingEnergyDao.findNaturalByDate(rangeStatistics.getDatetime(), rangeStatistics.getBegin(), rangeStatistics.getEnd());
                List<DateStatistics> TotalNatural = WidgetUtil.mergeByEnergyPrice(vapour);
                vapour.stream().forEach(d -> {
                    if (natural.isEmpty()) {
                        natural.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (TotalNatural.isEmpty() || TotalNatural.get(0).getNumber() == 0.00) {
                        TotalNatural.add(new DateStatistics(0L, "", 1.00));
                        TotalNatural.get(0).setNumber(1.00);
                    }
                    Double unitPrice = natural.stream().findFirst().get().getNumber() / TotalNatural
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                });

                // 4：压缩空气
                // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
                List<DateByStatistics> daoTotalByCompress = daoTotalCost
                        .stream()
                        .filter(d -> d.getHour().equals("4"))
                        .collect(Collectors.toList());
                // 得到压缩空气总用电
                List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
                // 得到压缩空气总能耗
                List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
                compress.stream().forEach(d -> {
                    if (setScheme == true) {
                        if (compressByElectricity.isEmpty()) {
                            compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                        }
                        if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                            cooling.add(new DateStatistics(0L, "", 1.00));
                            cooling.get(0).setNumber(1.00);
                        }
                        Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                                .stream()
                                .findFirst()
                                .get()
                                .getNumber();
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                    if (setScheme == false) {
                        Double unitPrice = 0.00;
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                });

                // 热能(冷却水)
                // 电费 -> 1：设备类型为空调热能(冷却水)的
                List<DateByStatistics> daoTotalByCooling = daoTotalCost
                        .stream()
                        .filter(d -> d.getHour().equals("1"))
                        .collect(Collectors.toList());
                // 得到冷却水总用电
                List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
                // 得到冷却水总能耗
                List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
                air.stream().forEach(d -> {
                    if (setScheme == true) {
                        if (coolingByElectricity.isEmpty()) {
                            coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                        }
                        if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                            coolingCount.add(new DateStatistics(0L, "", 1.00));
                            coolingCount.get(0).setNumber(1.00);
                        }
                        Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                                .stream()
                                .findFirst()
                                .get()
                                .getNumber();
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                    if (setScheme == false) {
                        // 设定冷却水的单价为0.4
                        Double unitPrice = 0.40;
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                });

                // 自来水(软化水) * 单价
                softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), w
                        .getCount()
                        .intValue(), floorId, finalBuildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            }

            // 电能
            electricity.stream().forEach(e -> {
                e.setNumber(new BigDecimal(e.getNumber() / 10000).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
            List<DateStatistics> el = WidgetUtil.mergeElementByOld(electricity
                                                                           .stream()
                                                                           .map(d -> new DateStatistics(d
                                                                                                                .getType()
                                                                                                                .longValue(), d.getDate(), d.getNumber()))
                                                                           .collect(Collectors.toList()));

            // 车间能耗总和
            List<DateStatistics> allEnergyByWorkshop = new ArrayList<>();

            // 采购的数据（平帐）---
            List<PurchasingBody> PO = purchasingEnergyDao.findPurchaseByEnergyCount(finalBuildingId, floorId, w
                    .getCount()
                    .intValue(), null, rangeStatistics.getBegin().toString(), rangeStatistics
                                                                                            .getEnd()
                                                                                            .toString(), rangeStatistics.getFormat());
            if (!PO.isEmpty()) {
                // 冷却水
                List<DateStatistics> coolingPO = new ArrayList<>();
                // 软化水
                List<DateStatistics> softeningPO = new ArrayList<>();
                // 压缩空气
                List<DateStatistics> compressPO = new ArrayList<>();
                // 蒸汽
                List<DateStatistics> vapourPO = new ArrayList<>();
                // 用电
                List<DateStatistics> electricityPO = new ArrayList<>();

                // 1：费用值 Price
                if (unit == 1) {
                    // 冷却水 coolingPO + air
                    coolingPO = PO
                            .stream()
                            .filter(p -> p.getType() == 1)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    air = WidgetUtil.mergePOByDate(coolingPO, air);

                    // 软化水 softeningPO + softening
                    softeningPO = PO
                            .stream()
                            .filter(p -> p.getType() == 2)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    softening = WidgetUtil.mergePOByDate(softeningPO, softening);

                    // 压缩空气 compressPO + compress
                    compressPO = PO
                            .stream()
                            .filter(p -> p.getType() == 4)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    compress = WidgetUtil.mergePOByDate(compressPO, compress);

                    // 蒸汽 vapourPO + vapour
                    vapourPO = PO
                            .stream()
                            .filter(p -> p.getType() == 6)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    vapour = WidgetUtil.mergePOByDate(vapourPO, vapour);

                    // 用电 electricityPO + el
                    electricityPO = PO
                            .stream()
                            .filter(p -> p.getType() == 7 || p.getType() == 8 || p.getType() == 9 || p.getType() == 10)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    el = WidgetUtil.mergePOByDate(electricityPO, el);
                }

            }
            // ---采购

            vapour.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            compress.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            air.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            softening.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            el.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });

            Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
            result.put("workshopData", WidgetUtil.findDateStatistics(rangeStatistics.getDates(), WidgetUtil.mergeElementByOld(allEnergyByWorkshop)));

            mapResult.put(w.getDate(), result);
        });

        mapResult.forEach((key, value) -> {
            value.forEach((k, v) -> {
                if (key.equals("公共")) {
                    workshopByWe.put("公共车间", v);
                }
            });
        });

        workshopByWe.forEach((k, v) -> {
            v.stream().forEach(d -> {
                mapResult.forEach((key, value) -> {
                    value.forEach((ke2, ve2) -> {
                        if (key.equals("公共")) {
                            // 删除公共
                        }
                        ve2.stream().forEach(obj -> {
                            if (obj.getDate().equals(d.getDate())) {
                                obj.setNumber(new BigDecimal(obj.getNumber() + (d.getNumber() / (mapResult.size() - 1)))
                                                      .setScale(4, BigDecimal.ROUND_HALF_UP)
                                                      .doubleValue());
                            }
                        });
                    });
                });
            });
        });

        mapResult.remove("公共");

        // 公共查单个车间
        List<DateStatistics> workMag = workshopDao.queryByWorkId(workshopId);
        if (workMag.size() <= 1) {
            if (Objects.isNull(range) && !Objects.isNull(workshopId)) {
                Map<String, Map<String, List<DateStatistics>>> ss = findWorkshopCostOf(workshopId, null, null, range, rangeStatistics.getBegin(), rangeStatistics.getEnd(), unit);
                AtomicReference<Double> countP = new AtomicReference<>(0.00);
                AtomicReference<Integer> lens = new AtomicReference<>(1);
                if (!Objects.isNull(ss)) {
                    ss.forEach((k, v) -> {
                        v.forEach((k1, v1) -> {
                            countP.set(v1.get(0).getNumber());
                        });
                    });
                }
                if (!Objects.isNull(mapResult)) {
                    mapResult.forEach((k, v) -> {
                        v.forEach((k1, v1) -> {
                            lens.set(v1.size());
                        });
                    });
                }

                Double finalCountP = countP.get();
                Integer finalLens = lens.get();
                mapResult.forEach((kk, vv) -> {
                    vv.forEach((v1, v2) -> {
                        v2.stream().forEach(len -> {
                            len.setNumber(WidgetUtil.setPriceByTwoDecimals(finalCountP / finalLens));
                        });
                    });
                });
            }
            return mapResult
                    .entrySet()
                    .stream()
                    .filter(map -> map.getKey().equals(workMag.stream().findFirst().get().getDate()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 非管理员只能查看部门下的车间
        if (!user.isAdmin()) {
            List<Workshop> workshops1 = workshopDao.findAllWorkshopByDeptId(SecurityUtils.getDeptId().intValue(), null);
            List<String> stringList = new ArrayList<>();
            workshops1.stream().forEach(w -> {
                stringList.add(w.getShopName());
            });
            mapResult.entrySet().removeIf(entry -> !stringList.contains(entry.getKey()));
        }

        return mapResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, List<DateStatistics>> findTotalMedium(Integer workshopId, Integer floorId, Integer buildingId, String range, LocalDate begin, LocalDate end, Integer unit) {

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end, "%Y-%m-%d", "'%Y-%m-%d'");

        if (!Objects.isNull(workshopId)) {
            buildingId = null;
        }

        // 用电
        List<DateByStatistics> electricity = new ArrayList<>();
        // 所有能耗
        List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
        // 4：压缩空气
        List<DateStatistics> compress = allEnergy.stream().filter(d -> d.getCount() == 4L).collect(Collectors.toList());
        // 1：冷却水(空调热能)
        List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
        // 2：软化水(自来水)
        List<DateStatistics> softening = allEnergy
                .stream()
                .filter(d -> d.getCount() == 2L)
                .collect(Collectors.toList());
        // 默认单位：能耗
        if (unit == 0) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

        }
        // 能耗价格：万元
        if (unit == 1) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 查询所有单价(查找8为峰电、9为平电、10为谷电的单价)
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

            // 4：压缩空气
            // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
            List<DateByStatistics> daoTotalByCompress = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("4"))
                    .collect(Collectors.toList());
            // 得到压缩空气总用电
            List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
            // 得到压缩空气总能耗
            List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
            compress.stream().forEach(d -> {
                if (setScheme == true) {
                    if (compressByElectricity.isEmpty()) {
                        compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                        cooling.add(new DateStatistics(0L, "", 1.00));
                        cooling.get(0).setNumber(1.00);
                    }
                    Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    Double unitPrice = 0.00;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 热能(冷却水)
            // 电费 -> 1：设备类型为空调热能(冷却水)的
            List<DateByStatistics> daoTotalByCooling = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("1"))
                    .collect(Collectors.toList());
            // 得到冷却水总用电
            List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
            // 得到冷却水总能耗
            List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
            air.stream().forEach(d -> {
                if (setScheme == true) {
                    if (coolingByElectricity.isEmpty()) {
                        coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                        coolingCount.add(new DateStatistics(0L, "", 1.00));
                        coolingCount.get(0).setNumber(1.00);
                    }
                    Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    // 设定冷却水的单价为0.4
                    Double unitPrice = 0.40;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 自来水(软化水) * 单价
            softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 电能(万元)
            electricity.stream().forEach(e -> {
                e.setNumber(new BigDecimal(e.getNumber() / 10000).setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
        }

        // 电能
        List<DateStatistics> el = WidgetUtil.mergeElementByOld(electricity
                                                                       .stream()
                                                                       .map(d -> new DateStatistics(d
                                                                                                            .getType()
                                                                                                            .longValue(), d.getDate(), d.getNumber()))
                                                                       .collect(Collectors.toList()));

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        Map<String, List<DateStatistics>> stringListMap = this.findConjunctByEnergy(el, compress, softening, air, range, rangeStatistics.getBegin(), rangeStatistics.getEnd(), unit);

        // 能源采购加入能耗排名（只计算价格，unit始终是传入1）
        // 采购的数据（平帐）---
        List<PurchasingBody> PO = purchasingEnergyDao.findPurchaseByEnergyCount(buildingId, floorId, workshopId, null, rangeStatistics
                .getBegin()
                .toString(), rangeStatistics.getEnd().toString(), rangeStatistics.getFormat());
        if (!PO.isEmpty()) {
            // 冷却水
            List<DateStatistics> coolingPO = new ArrayList<>();
            // 软化水
            List<DateStatistics> softeningPO = new ArrayList<>();
            // 压缩空气
            List<DateStatistics> compressPO = new ArrayList<>();
            // 用电
            List<DateStatistics> electricityPO = new ArrayList<>();

            // 1：费用值 Price
            if (unit == 1) {
                // 冷却水 coolingPO + air
                coolingPO = PO
                        .stream()
                        .filter(p -> p.getType() == 1)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                air = WidgetUtil.mergePOByDate(coolingPO, air);

                // 软化水 softeningPO + softening
                softeningPO = PO
                        .stream()
                        .filter(p -> p.getType() == 2)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                softening = WidgetUtil.mergePOByDate(softeningPO, softening);

                // 压缩空气 compressPO + compress
                compressPO = PO
                        .stream()
                        .filter(p -> p.getType() == 4)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                compress = WidgetUtil.mergePOByDate(compressPO, compress);

                // 用电 electricityPO + el
                electricityPO = PO
                        .stream()
                        .filter(p -> p.getType() == 7 || p.getType() == 8 || p.getType() == 9 || p.getType() == 10)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                el = WidgetUtil.mergePOByDate(electricityPO, el);
            }

        }
        // --- 采购

        result.put("electricList", WidgetUtil.findDateStatistics(rangeStatistics.getDates(), WidgetUtil.mergeElementByOld(el)));
        result.put("compressedAirData", WidgetUtil.findDateStatistics(rangeStatistics.getDates(), WidgetUtil.mergeElementByOld(compress)));
        result.put("waterData", WidgetUtil.findDateStatistics(rangeStatistics.getDates(), WidgetUtil.mergeElementByOld(softening)));
        result.put("thermalList", WidgetUtil.findDateStatistics(rangeStatistics.getDates(), WidgetUtil.mergeElementByOld(air)));
        return result;
    }

    @Override
    public List<DateStatistics> findDateStatistics(List<String> dates, List<DateStatistics> statistics) {
        Map<String, Long> map = statistics
                .stream()
                .collect(Collectors.toMap(DateStatistics::getDate, DateStatistics::getCount));
        return dates
                .stream()
                .map(d -> new DateStatistics(map.getOrDefault(d, 0L), d, 0.00))
                .collect(Collectors.toList());
    }

    @Override
    public List<DateStatistics> findPriceStatistics(List<DateStatistics> dateStatistics, Integer unit, Integer type) {

        dateStatistics.stream().forEach(o -> {
            if (energyPriceSettingDao.selectAllEnergyPriceSettingByCount(type) != 0) {
                double price = 0.00;
                price = energyPriceSettingDao.selectAllEnergyPriceSettingByType(type);
                if (o.getCount().intValue() != 0 && price != 0.00) {
                    if (unit == 0) {
                        o.setNumber(new BigDecimal(o.getCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    }
                    if (unit == 1) {
                        o.setNumber(new BigDecimal(price * o.getCount() / 10000)
                                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                }
            }
        });

        return dateStatistics;
    }

    @Override
    public DateStatistics findPriceStatistic(DateStatistics dateStatistics, Integer unit, Integer type) {

        if (energyPriceSettingDao.selectAllEnergyPriceSettingByCount(type) != 0) {
            double price = 0.00;
            price = energyPriceSettingDao.selectAllEnergyPriceSettingByType(type);
            if (dateStatistics.getCount().intValue() != 0 && price != 0.00) {
                if (unit == 0) {
                    dateStatistics.setNumber(new BigDecimal(dateStatistics.getCount())
                                                     .setScale(2, BigDecimal.ROUND_HALF_UP)
                                                     .doubleValue());
                }
                if (unit == 1) {
                    dateStatistics.setNumber(new BigDecimal(price * dateStatistics.getCount() / 10000)
                                                     .setScale(2, BigDecimal.ROUND_HALF_UP)
                                                     .doubleValue());
                }
            }
        }

        return dateStatistics;
    }

    @Override
    public Map<String, List<DateStatistics>> findAllEnergy(Map<String, List<DateStatistics>> statistics) {

        List<DateStatistics> waterData = new ArrayList<>();
        List<DateStatistics> compressedAirData = new ArrayList<>();
        List<DateStatistics> electricData = new ArrayList<>();
        List<DateStatistics> thermalData = new ArrayList<>();

        statistics.forEach((key, value) -> {
            value.stream().forEach(v -> {
                if (v.getCount() == 1 || v.getCount() == 2 || v.getCount() == 3) {
                    v.setCount(v.getNumber().longValue());
                    waterData.add(v);
                }
                if (v.getCount() == 4 || v.getCount() == 5 || v.getCount() == 6) {
                    v.setCount(v.getNumber().longValue());
                    compressedAirData.add(v);
                }
                if (v.getCount() == 7 || v.getCount() == 8 || v.getCount() == 9 || v.getCount() == 10) {
                    v.setCount(v.getNumber().longValue());
                    electricData.add(v);
                }
                if (v.getCount() == 11) {
                    v.setCount(v.getNumber().longValue());
                    thermalData.add(v);
                }
            });

        });

        List<DateStatistics> waterList = new ArrayList<>();
        List<DateStatistics> compressedAirList = new ArrayList<>();
        List<DateStatistics> electricList = new ArrayList<>();
        List<DateStatistics> thermalList = new ArrayList<>();

        compressedAirData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(compressedAirList::add);
                });
        waterData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(waterList::add);
                });
        electricData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(electricList::add);
                });
        thermalData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(thermalList::add);
                });


        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("waterData", waterList);
        result.put("compressedAirData", compressedAirData);
        result.put("electricList", electricList);
        result.put("thermalList", thermalList);

        return result;
    }


    @Override
    public Map<String, List<DateStatistics>> findAllEnergyTwo(List<String> dates, Map<String, List<DateStatistics>> statistics, String range) {

        List<DateStatistics> waterData = new ArrayList<>();
        List<DateStatistics> compressedAirData = new ArrayList<>();
        List<DateStatistics> electricData = new ArrayList<>();
        List<DateStatistics> thermalData = new ArrayList<>();

        statistics.forEach((key, value) -> {
            value.stream().forEach(v -> {
                if (v.getCount() == 1 || v.getCount() == 2 || v.getCount() == 3) {
                    v.setCount(v.getNumber().longValue());
                    waterData.add(v);
                }
                if (v.getCount() == 4 || v.getCount() == 5 || v.getCount() == 6) {
                    v.setCount(v.getNumber().longValue());
                    compressedAirData.add(v);
                }
                if (v.getCount() == 7 || v.getCount() == 8 || v.getCount() == 9 || v.getCount() == 10) {
                    v.setCount(v.getNumber().longValue());
                    electricData.add(v);
                }
                if (v.getCount() == 11) {
                    v.setCount(v.getNumber().longValue());
                    thermalData.add(v);
                }
            });

        });

        List<DateStatistics> waterList = new ArrayList<>();
        List<DateStatistics> compressedAirList = new ArrayList<>();
        List<DateStatistics> electricList = new ArrayList<>();
        List<DateStatistics> thermalList = new ArrayList<>();

        compressedAirData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getNumber().longValue() + b
                                    .getNumber()
                                    .longValue(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(compressedAirList::add);
                });
        waterData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getNumber().longValue() + b
                                    .getNumber()
                                    .longValue(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(waterList::add);
                });
        electricData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getNumber().longValue() + b
                                    .getNumber()
                                    .longValue(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(electricList::add);
                });
        thermalData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getNumber().longValue() + b
                                    .getNumber()
                                    .longValue(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(thermalList::add);
                });

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("waterData", findSortDate(findSetEnergy(findDateStatistics(dates, waterList)), range));
        result.put("compressedAirData", findSortDate(findSetEnergy(findDateStatistics(dates, compressedAirList)), range));
        result.put("electricList", findSortDate(findSetEnergy(findDateStatistics(dates, electricList)), range));
        result.put("thermalList", findSortDate(findSetEnergy(findDateStatistics(dates, thermalList)), range));

        return result;
    }

    @Override
    public List<DateStatistics> findSortDate(List<DateStatistics> dateStatistics, String range) {
        dateStatistics.sort((o1, o2) -> {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            if (MONTH.equals(range)) {
                format2 = new SimpleDateFormat("yyyy-MM");
            }
            try {
                Date dt1 = format2.parse(o1.getDate());
                Date dt2 = format2.parse(o2.getDate());
                return Long.compare(dt1.getTime(), dt2.getTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        });
        return dateStatistics;
    }

    ;

    @Override
    public Map<String, List<DateStatistics>> findAllSystemEnergy(Map<String, List<DateStatistics>> statistics) {

        List<DateStatistics> lightingData = new ArrayList<>();
        List<DateStatistics> conditionerData = new ArrayList<>();
        List<DateStatistics> steamData = new ArrayList<>();
        List<DateStatistics> freezeData = new ArrayList<>();

        statistics.forEach((key, value) -> {
            if ("lighting".equals(key)) {
                value.forEach(s -> {
                    lightingData.add(s);
                });
            }

            if ("conditioner".equals(key)) {
                value.forEach(s -> {
                    conditionerData.add(s);
                });
            }

            if ("steam".equals(key)) {
                value.forEach(s -> {
                    steamData.add(s);
                });
            }

            if ("freeze".equals(key)) {
                value.forEach(s -> {
                    freezeData.add(s);
                });
            }

        });

        List<DateStatistics> lightingList = new ArrayList<>();
        List<DateStatistics> conditionerList = new ArrayList<>();
        List<DateStatistics> steamList = new ArrayList<>();
        List<DateStatistics> freezeList = new ArrayList<>();

        lightingData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(lightingList::add);
                });
        conditionerData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(conditionerList::add);
                });
        steamData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(steamList::add);
                });
        freezeData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(freezeList::add);
                });


        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("lightingData", lightingList);
        result.put("conditionerData", conditionerList);
        result.put("steamData", steamList);
        result.put("freezeData", freezeList);

        return result;
    }

    @Override
    public Map<String, List<DateStatistics>> findWorkshopEnergy(Map<String, List<DateStatistics>> statistics, String range) {

        List<DateStatistics> workshopData = new ArrayList<>();
        List<DateStatistics> workshopList = new ArrayList<>();

        statistics.forEach((key, value) -> {
            value.forEach(s -> {
                workshopData.add(s);
            });
        });

        workshopData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                                    .doubleValue()))
                            .ifPresent(workshopList::add);
                });


        workshopList.sort((o1, o2) -> {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            if (MONTH.equals(range)) {
                format2 = new SimpleDateFormat("yyyy-MM");
            }
            try {
                Date dt1 = format2.parse(o1.getDate());
                Date dt2 = format2.parse(o2.getDate());
                return Long.compare(dt1.getTime(), dt2.getTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        });

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("workshopData", workshopList);

        return result;
    }

    @Override
    public Map<String, List<DateStatistics>> findWorkshopProportion(Map<String, List<DateStatistics>> statistics) {

        List<DateStatistics> workshopData = new ArrayList<>();
        List<DateStatistics> workshopList = new ArrayList<>();

        statistics.forEach((key, value) -> {
            value.forEach(s -> {
                s.setDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                workshopData.add(s);
            });
        });

        workshopData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                    .setScale(2, BigDecimal.ROUND_HALF_UP)
                                    .doubleValue()))
                            .ifPresent(workshopList::add);
                });

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("workshopData", workshopList);

        return result;
    }

    @Override
    public Map<String, Map<String, List<DateStatistics>>> findWorkshopCostOf(Long workshopId, Integer floorId, Integer buildingId, String range, LocalDate begin, LocalDate end, Integer unit) {

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end.plusDays(-1), "%Y-%m-%d", "'%Y-%m-%d'");

        Map<String, Map<String, List<DateStatistics>>> mapResult = new LinkedHashMap<>();

        List<DateStatistics> work = workshopDao.queryByWorkId(null);
        if (!Objects.isNull(workshopId)) {
            work = workshopDao.queryByWorkId(workshopId);
        }

        // 展示楼栋下的车间
        if (Objects.isNull(workshopId) && Objects.isNull(floorId) && !Objects.isNull(buildingId)) {
            work = workshopDao.findWorkshopByBuildingId(buildingId);
        }
        // 展示楼层下的车间
        if (Objects.isNull(workshopId) && !Objects.isNull(floorId) && !Objects.isNull(buildingId)) {
            work = workshopDao.findWorkshopByFloorId(floorId);
        }

        final String date = rangeStatistics.getDatetime();
        final LocalDate beginOn = rangeStatistics.getBegin();
        final LocalDate endOn = rangeStatistics.getEnd();

        work.stream().forEach(w -> {
            // 用电
            List<DateByStatistics> electricity = new ArrayList<>();
            // 所有能耗
            List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), w
                    .getCount()
                    .intValue(), null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 6：蒸汽
            List<DateStatistics> vapour = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 6L)
                    .collect(Collectors.toList());
            // 4：压缩空气
            List<DateStatistics> compress = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 4L)
                    .collect(Collectors.toList());
            // 1：冷却水(空调热能)
            List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
            // 2：软化水(自来水)
            List<DateStatistics> softening = allEnergy
                    .stream()
                    .filter(d -> d.getCount() == 2L)
                    .collect(Collectors.toList());
            // 默认单位：能耗
            if (unit == 0) {
                // 用电
                List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), w
                        .getCount()
                        .intValue(), null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

            }
            // 能耗价格：万元
            if (unit == 1) {
                List<DateByStatistics> daoTotalCost = new ArrayList<>();

                // 数据量过大的时候查询月的时候调用年表
                if ("month".equals(range) && Objects.isNull(w.getCount())) {
                    daoTotalCost = deviceEnergyMapper.findConsumeElectricityPriceByYear(rangeStatistics.getDatetime(), w
                            .getCount()
                            .intValue(), null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                    electricity = WidgetUtil.mergeByDateBy(daoTotalCost);
                } else {
                    // 用电
                    daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), w
                            .getCount()
                            .intValue(), null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                    electricity = WidgetUtil.mergeByDateBy(daoTotalCost);
                }

                // 蒸汽
                // 天然气费 天然气费(一个月)÷蒸汽用量(一个月)=单价x蒸汽能耗m3
                // 天然气费用 natural
                List<DateStatistics> natural = purchasingEnergyDao.findNaturalByDate(rangeStatistics.getDatetime(), rangeStatistics.getBegin(), rangeStatistics.getEnd());
                List<DateStatistics> TotalNatural = WidgetUtil.mergeByEnergyPrice(vapour);
                vapour.stream().forEach(d -> {
                    if (natural.isEmpty()) {
                        natural.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (TotalNatural.isEmpty() || TotalNatural.get(0).getNumber() == 0.00) {
                        TotalNatural.add(new DateStatistics(0L, "", 1.00));
                        TotalNatural.get(0).setNumber(1.00);
                    }
                    Double unitPrice = natural.stream().findFirst().get().getNumber() / TotalNatural
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                });

                // 4：压缩空气
                // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
                List<DateByStatistics> daoTotalByCompress = daoTotalCost
                        .stream()
                        .filter(d -> d.getHour().equals("4"))
                        .collect(Collectors.toList());
                // 得到压缩空气总用电
                List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
                // 得到压缩空气总能耗
                List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
                compress.stream().forEach(d -> {
                    if (setScheme == true) {
                        if (compressByElectricity.isEmpty()) {
                            compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                        }
                        if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                            cooling.add(new DateStatistics(0L, "", 1.00));
                            cooling.get(0).setNumber(1.00);
                        }
                        Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                                .stream()
                                .findFirst()
                                .get()
                                .getNumber();
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                    if (setScheme == false) {
                        Double unitPrice = 0.00;
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                });

                // 热能(冷却水)
                // 电费 -> 1：设备类型为空调热能(冷却水)的
                List<DateByStatistics> daoTotalByCooling = daoTotalCost
                        .stream()
                        .filter(d -> d.getHour().equals("1"))
                        .collect(Collectors.toList());
                // 得到冷却水总用电
                List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
                // 得到冷却水总能耗
                List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
                air.stream().forEach(d -> {
                    if (setScheme == true) {
                        if (coolingByElectricity.isEmpty()) {
                            coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                        }
                        if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                            coolingCount.add(new DateStatistics(0L, "", 1.00));
                            coolingCount.get(0).setNumber(1.00);
                        }
                        Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                                .stream()
                                .findFirst()
                                .get()
                                .getNumber();
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                    if (setScheme == false) {
                        // 设定冷却水的单价为0.4
                        Double unitPrice = 0.40;
                        d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                            .setScale(4, BigDecimal.ROUND_HALF_UP)
                                            .doubleValue());
                    }
                });

                // 自来水(软化水) * 单价
                softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), w
                        .getCount()
                        .intValue(), null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
                electricity.stream().forEach(e -> {
                    e.setNumber(new BigDecimal(e.getNumber() / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                });
            }

            // 电能
            List<DateStatistics> el = WidgetUtil.mergeElementByOld(electricity
                                                                           .stream()
                                                                           .map(d -> new DateStatistics(d
                                                                                                                .getType()
                                                                                                                .longValue(), d.getDate(), d.getNumber()))
                                                                           .collect(Collectors.toList()));

            // 车间能耗总和
            List<DateStatistics> allEnergyByWorkshop = new ArrayList<>();

            // 采购的数据（平帐）---
            List<PurchasingBody> PO = purchasingEnergyDao.findPurchaseByEnergyCount(buildingId, floorId, w
                    .getCount()
                    .intValue(), null, rangeStatistics.getBegin().toString(), rangeStatistics
                                                                                            .getEnd()
                                                                                            .toString(), rangeStatistics.getFormat());
            if (!PO.isEmpty()) {
                // 冷却水
                List<DateStatistics> coolingPO = new ArrayList<>();
                // 软化水
                List<DateStatistics> softeningPO = new ArrayList<>();
                // 压缩空气
                List<DateStatistics> compressPO = new ArrayList<>();
                // 蒸汽
                List<DateStatistics> vapourPO = new ArrayList<>();
                // 用电
                List<DateStatistics> electricityPO = new ArrayList<>();

                // 1：费用值 Price
                if (unit == 1) {
                    // 冷却水 coolingPO + air
                    coolingPO = PO
                            .stream()
                            .filter(p -> p.getType() == 1)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    air = WidgetUtil.mergePOByDate(coolingPO, air);

                    // 软化水 softeningPO + softening
                    softeningPO = PO
                            .stream()
                            .filter(p -> p.getType() == 2)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    softening = WidgetUtil.mergePOByDate(softeningPO, softening);

                    // 压缩空气 compressPO + compress
                    compressPO = PO
                            .stream()
                            .filter(p -> p.getType() == 4)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    compress = WidgetUtil.mergePOByDate(compressPO, compress);

                    // 蒸汽 vapourPO + vapour
                    vapourPO = PO
                            .stream()
                            .filter(p -> p.getType() == 6)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    vapour = WidgetUtil.mergePOByDate(vapourPO, vapour);

                    // 用电 electricityPO + el
                    electricityPO = PO
                            .stream()
                            .filter(p -> p.getType() == 7 || p.getType() == 8 || p.getType() == 9 || p.getType() == 10)
                            .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                            .collect(Collectors.toList());
                    el = WidgetUtil.mergePOByDate(electricityPO, el);
                }

            }
            // ---采购

            vapour.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            compress.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            air.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            softening.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });
            el.stream().forEach(v -> {
                allEnergyByWorkshop.add(v);
            });

            Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
            result.put("workshopData", WidgetUtil.mergeElementByNow(allEnergyByWorkshop));
            if (WidgetUtil.mergeElementByNow(allEnergyByWorkshop).isEmpty()) {
                List<DateStatistics> dateStatistics = new ArrayList<>();
                dateStatistics.add(new DateStatistics(0L, DateTimeFormatter
                        .ofPattern("yyyy-MM-dd")
                        .format(LocalDate.now()), 0.00));
                result.put("workshopData", dateStatistics);
            }

            mapResult.put(w.getDate(), result);
        });

        Map<String, List<DateStatistics>> workshopByWe = new LinkedHashMap<>();

        mapResult.forEach((k, v) -> {
            v.forEach((key, value) -> {
                if ("公共".equals(k)) {
                    workshopByWe.put("公共车间", value);
                }
            });
        });

        workshopByWe.forEach((k, v) -> {
            v.stream().forEach(d -> {
                mapResult.forEach((key, value) -> {
                    value.forEach((ke2, ve2) -> {
                        if (key.equals("公共")) {
                            // 删除公共
                        }
                        ve2.stream().forEach(obj -> {
                            if (obj.getDate().equals(d.getDate())) {
                                obj.setNumber(new BigDecimal(obj.getNumber() + (d.getNumber() / (mapResult.size() - 1)))
                                                      .setScale(4, BigDecimal.ROUND_HALF_UP)
                                                      .doubleValue());
                            }
                        });
                    });
                });
            });
        });

        mapResult.remove("公共");

        // 公共查单个车间
        List<DateStatistics> workMag = workshopDao.queryByWorkId(workshopId);
        if (workMag.size() <= 1) {
            return mapResult
                    .entrySet()
                    .stream()
                    .filter(map -> map.getKey().equals(workMag.stream().findFirst().get().getDate()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 非管理员只能查看部门下的车间
        if (!user.isAdmin()) {
            List<Workshop> workshops1 = workshopDao.findAllWorkshopByDeptId(SecurityUtils.getDeptId().intValue(), null);
            List<String> stringList = new ArrayList<>();
            workshops1.stream().forEach(w -> {
                stringList.add(w.getShopName());
            });
            mapResult.entrySet().removeIf(entry -> !stringList.contains(entry.getKey()));
        }

        return mapResult;
    }

    @Override
    public Map<String, List<DateStatistics>> findMediumCostOf(Integer workshopId, Integer floorId, Integer buildingId, String range, LocalDate begin, LocalDate end, Integer unit) {

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end.plusDays(-1), "%Y-%m-%d", "'%Y-%m-%d'");

        if (!Objects.isNull(workshopId)) {
            buildingId = null;
        }

        // 用电
        List<DateByStatistics> electricity = new ArrayList<>();
        // 所有能耗
        List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
        // 4：压缩空气
        List<DateStatistics> compress = allEnergy.stream().filter(d -> d.getCount() == 4L).collect(Collectors.toList());
        // 1：冷却水(空调热能)
        List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
        // 2：软化水(自来水)
        List<DateStatistics> softening = allEnergy
                .stream()
                .filter(d -> d.getCount() == 2L)
                .collect(Collectors.toList());
        // 默认单位：能耗
        if (unit == 0) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

        }
        // 能耗价格：万元
        if (unit == 1) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 查询所有单价(查找8为峰电、9为平电、10为谷电的单价)
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

            // 4：压缩空气
            // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
            List<DateByStatistics> daoTotalByCompress = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("4"))
                    .collect(Collectors.toList());
            // 得到压缩空气总用电
            List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
            // 得到压缩空气总能耗
            List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
            compress.stream().forEach(d -> {
                if (setScheme == true) {
                    if (compressByElectricity.isEmpty()) {
                        compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                        cooling.add(new DateStatistics(0L, "", 1.00));
                        cooling.get(0).setNumber(1.00);
                    }
                    Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    Double unitPrice = 0.00;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 热能(冷却水)
            // 电费 -> 1：设备类型为空调热能(冷却水)的
            List<DateByStatistics> daoTotalByCooling = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("1"))
                    .collect(Collectors.toList());
            // 得到冷却水总用电
            List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
            // 得到冷却水总能耗
            List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
            air.stream().forEach(d -> {
                if (setScheme == true) {
                    if (coolingByElectricity.isEmpty()) {
                        coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                        coolingCount.add(new DateStatistics(0L, "", 1.00));
                        coolingCount.get(0).setNumber(1.00);
                    }
                    Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    // 设定冷却水的单价为0.4
                    Double unitPrice = 0.40;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 自来水(软化水) * 单价
            softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), workshopId, floorId, buildingId, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 电能(万元)
            electricity.stream().forEach(e -> {
                e.setNumber(new BigDecimal(e.getNumber() / 10000).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
        }

        // 采购的数据（平帐）---
        List<PurchasingBody> PO = purchasingEnergyDao.findPurchaseByEnergyCount(buildingId, floorId, workshopId, null, rangeStatistics
                .getBegin()
                .toString(), rangeStatistics.getEnd().toString(), rangeStatistics.getFormat());
        if (!PO.isEmpty()) {
            // 冷却水
            List<DateStatistics> coolingPO = new ArrayList<>();
            // 软化水
            List<DateStatistics> softeningPO = new ArrayList<>();
            // 压缩空气
            List<DateStatistics> compressPO = new ArrayList<>();
            // 蒸汽
            List<DateStatistics> vapourPO = new ArrayList<>();

            // 1：费用值 Price
            if (unit == 1) {
                // 冷却水 coolingPO + air
                coolingPO = PO
                        .stream()
                        .filter(p -> p.getType() == 1)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                air = WidgetUtil.mergePOByDate(coolingPO, air);

                // 软化水 softeningPO + softening
                softeningPO = PO
                        .stream()
                        .filter(p -> p.getType() == 2)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                softening = WidgetUtil.mergePOByDate(softeningPO, softening);

                // 压缩空气 compressPO + compress
                compressPO = PO
                        .stream()
                        .filter(p -> p.getType() == 4)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                compress = WidgetUtil.mergePOByDate(compressPO, compress);
            }

        }

        // 电能
        List<DateStatistics> el = WidgetUtil.mergeElementByNow(electricity
                                                                       .stream()
                                                                       .map(d -> new DateStatistics(d
                                                                                                            .getType()
                                                                                                            .longValue(), d.getDate(), d.getNumber()))
                                                                       .collect(Collectors.toList()));
        // 压缩空气
        List<DateStatistics> co = WidgetUtil.mergeElementByNow(compress);
        // 冷却水(空调热能)
        List<DateStatistics> cl = WidgetUtil.mergeElementByNow(air);
        // 软化水
        List<DateStatistics> so = WidgetUtil.mergeElementByNow(softening);

        // 能源采购计算电能的
        if (!PO.isEmpty()) {
            // 用电
            List<DateStatistics> electricityPO = new ArrayList<>();
            // 1：费用值 Price
            if (unit == 1) {
                // 用电 electricityPO + el
                electricityPO = PO
                        .stream()
                        .filter(p -> p.getType() == 7 || p.getType() == 8 || p.getType() == 9 || p.getType() == 10)
                        .map(d -> new DateStatistics(0L, d.getDate(), d.getPrice()))
                        .collect(Collectors.toList());
                electricityPO.stream().forEach(e -> {
                    e.setDate(LocalDate.now().toString());
                });

                el = WidgetUtil.mergePOByDate(electricityPO, el);
            }
        }

        Map<String, List<DateStatistics>> stringListMap = this.findConjunctByEnergyCount(el, co, cl, so, range, rangeStatistics.getBegin(), rangeStatistics.getEnd(), unit);

        List<DateStatistics> statistics = new ArrayList<>();
        if (!el.isEmpty()) {
            el.get(0).setDate("用电");
            el.get(0).setCount(7L);
            statistics.add(el.get(0));
        } else {
            statistics.add(new DateStatistics(7L, "用电", 0.00));
        }
        if (!co.isEmpty()) {
            co.get(0).setDate("压缩空气");
            co.get(0).setCount(4L);
            statistics.add(co.get(0));
        } else {
            statistics.add(new DateStatistics(4L, "压缩空气", 0.00));
        }
        if (!cl.isEmpty()) {
            cl.get(0).setDate("空调热能");
            cl.get(0).setCount(11L);
            statistics.add(cl.get(0));
        } else {
            statistics.add(new DateStatistics(11L, "空调热能", 0.00));
        }
        if (!so.isEmpty()) {
            so.get(0).setDate("软化水");
            so.get(0).setCount(2L);
            statistics.add(so.get(0));
        } else {
            statistics.add(new DateStatistics(2L, "软化水", 0.00));
        }

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        result.put("energy", statistics);
        return result;
    }

    @Override
    public Map<String, List<DateStatistics>> findEquipmentEnergy(Long workshopId, String range, LocalDate begin, LocalDate end, Integer unit) {

        String format = "%Y-%m-%d";
        String datetime = "'%Y-%m-%d'";
        List<String> dates = new ArrayList();

        if (StrUtil.isEmpty(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            end = end.plusDays(1);
        }

        if (Day.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = LocalDate.now();
            end = begin.plusDays(1);
        } else if (WEEK.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = begin.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            end = end.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            end = end.plusDays(1);

        } else if (MONTH.equals(range)) {
            format = "%Y-%m";
            begin = begin.plusDays(-begin.getDayOfMonth() + 1);
            end = begin.plusMonths(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM").withZone(zone);

            datetime = "'%Y-%m'";
        }

        List<DateStatistics> energy = workshopDao.findEnergyStatisticsByAll(datetime, workshopId, begin, end);

        List<DateStatistics> lighting = energy
                .stream()
                .filter(v -> v.getNumber().longValue() == 12)
                .collect(Collectors.toList());
        List<DateStatistics> aConditioner = energy
                .stream()
                .filter(v -> v.getNumber().longValue() == 13)
                .collect(Collectors.toList());
        List<DateStatistics> steam = energy
                .stream()
                .filter(v -> v.getNumber().longValue() == 14)
                .collect(Collectors.toList());
        List<DateStatistics> freeze = energy
                .stream()
                .filter(v -> v.getNumber().longValue() == 15)
                .collect(Collectors.toList());


        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();
        result.put("lighting", findPriceStatistics(lighting, unit, 12));
        result.put("conditioner", findPriceStatistics(aConditioner, unit, 13));
        result.put("steam", findPriceStatistics(steam, unit, 14));
        result.put("freeze", findPriceStatistics(freeze, unit, 15));


        Map<String, List<DateStatistics>> countRes = findAllSystemEnergy(result);

        List<DateStatistics> workshopData = new ArrayList<>();
        List<DateStatistics> workshopList = new ArrayList<>();

        countRes.forEach((k, v) -> {
            v.forEach(s -> {
                s.setDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                workshopData.add(s);
            });
        });

        workshopData
                .parallelStream()
                .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                .forEach((id, transfer) -> {
                    transfer
                            .stream()
                            .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), a.getNumber() + b.getNumber()))
                            .ifPresent(workshopList::add);
                });

        countRes.forEach((k, v) -> {
            v.forEach(cr -> {
                workshopList.stream().forEach(w -> {
                    cr.setCount(cr.getNumber().longValue());
                    BigDecimal number = new BigDecimal(cr.getNumber() / new BigDecimal(w.getNumber())
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue() * 100);
                    double count = number.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    cr.setNumber(count);
                });
            });
            if (v.isEmpty()) {
                v.add(new DateStatistics(0L, LocalDateTime
                        .now()
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), 0.00));
            }
        });

        return countRes;
    }

    @Override
    public Map<String, List<DateStatistics>> findEnergyByType(String datetime, Long workshopId, LocalDate begin, LocalDate end, Integer byType) {

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        // 查询1-11的能耗类型数据，并计算好价格
        if (byType == 1) {
            List<DateStatistics> allEnergy = workshopDao.findAllEnergyByNo(datetime, workshopId, begin, end);
            result.put("energy", allEnergy);
        }
        if (byType == 2) {
            List<DateStatistics> allEnergy = workshopDao.findAllEnergyByWorkshopId(datetime, workshopId, begin, end);
            result.put("energy", allEnergy);
        }

        return result;
    }

    @Override
    public List<DateStatistics> findSetEnergy(List<DateStatistics> dateStatistics) {
        dateStatistics.stream().forEach(d -> {
            d.setNumber(d.getCount().doubleValue());
        });
        return dateStatistics;
    }

    @Override
    public RangeStatistics findDateByRange(String range, LocalDate begin, LocalDate end, String format, String datetime) {

        List<String> dates = new ArrayList();

        if (StrUtil.isEmpty(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        }

        if (Day.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = begin.plusDays(-begin.getDayOfMonth() + 1);
            end = end.plusDays(-end.getDayOfMonth() + 1);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        } else if (WEEK.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = begin.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            end = end.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        } else if (MONTH.equals(range)) {
            begin = begin.plusDays(-begin.getDayOfYear() + 1);
            end = begin.plusMonths(11);
            format = "%Y-%m";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM").withZone(zone);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusMonths(1)) {
                dates.add(formatter.format(date));
            }
            datetime = "'%Y-%m'";
        }
        return new RangeStatistics(dates, begin, end, format, datetime);
    }

    @Override
    public Map<String, List<DateStatistics>> findConjunctByEnergy(List<DateStatistics> electricList, List<DateStatistics> compressedAirData, List<DateStatistics> waterData, List<DateStatistics> thermalList, String range, LocalDate begin, LocalDate end, Integer unit) {
        // 计算公共车间的部分[设定公共车间为id = 15]
        Integer workshopId = 15;

        // 得到车间的数量 = workExtent[不等于公共车间的数量]
        List<DateStatistics> workMag = workshopDao.queryByWorkId(null);
        Integer workExtent = workMag
                .stream()
                .filter(w -> !"公共".equals(w.getDate()))
                .collect(Collectors.toList())
                .size();

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end, "%Y-%m-%d", "'%Y-%m-%d'");
        // 用电
        List<DateByStatistics> electricity = new ArrayList<>();
        // 所有能耗
        List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), workshopId, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
        // 4：压缩空气
        List<DateStatistics> compress = allEnergy.stream().filter(d -> d.getCount() == 4L).collect(Collectors.toList());
        // 1：冷却水(空调热能)
        List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
        // 2：软化水(自来水)
        List<DateStatistics> softening = allEnergy
                .stream()
                .filter(d -> d.getCount() == 2L)
                .collect(Collectors.toList());
        // 默认单位：能耗
        if (unit == 0) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), workshopId, null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

        }
        // 能耗价格：万元
        if (unit == 1) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), workshopId, null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 查询所有单价(查找8为峰电、9为平电、10为谷电的单价)
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

            // 4：压缩空气
            // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
            List<DateByStatistics> daoTotalByCompress = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("4"))
                    .collect(Collectors.toList());
            // 得到压缩空气总用电
            List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
            // 得到压缩空气总能耗
            List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
            compress.stream().forEach(d -> {
                if (setScheme == true) {
                    if (compressByElectricity.isEmpty()) {
                        compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                        cooling.add(new DateStatistics(0L, "", 1.00));
                        cooling.get(0).setNumber(1.00);
                    }
                    Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    Double unitPrice = 0.00;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 热能(冷却水)
            // 电费 -> 1：设备类型为空调热能(冷却水)的
            List<DateByStatistics> daoTotalByCooling = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("1"))
                    .collect(Collectors.toList());
            // 得到冷却水总用电
            List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
            // 得到冷却水总能耗
            List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
            air.stream().forEach(d -> {
                if (setScheme == true) {
                    if (coolingByElectricity.isEmpty()) {
                        coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                        coolingCount.add(new DateStatistics(0L, "", 1.00));
                        coolingCount.get(0).setNumber(1.00);
                    }
                    Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    // 设定冷却水的单价为0.4
                    Double unitPrice = 0.40;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 自来水(软化水) * 单价
            softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), workshopId, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 电能(万元)
            electricity.stream().forEach(e -> {
                e.setNumber(new BigDecimal(e.getNumber() / 10000).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
        }

        // 电能
        List<DateStatistics> el = WidgetUtil.mergeElementByOld(electricity
                                                                       .stream()
                                                                       .map(d -> new DateStatistics(d
                                                                                                            .getType()
                                                                                                            .longValue(), d.getDate(), d.getNumber()))
                                                                       .collect(Collectors.toList()));

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        // 处理接入的数据进行累加公共部分的数据
        el.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        compress.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        softening.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        air.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        electricList.addAll(el);
        compressedAirData.addAll(compress);
        waterData.addAll(softening);
        thermalList.addAll(air);

        return result;
    }

    @Override
    public Map<String, List<DateStatistics>> findConjunctByEnergyCount(List<DateStatistics> electricList, List<DateStatistics> compressedAirData, List<DateStatistics> waterData, List<DateStatistics> thermalList, String range, LocalDate begin, LocalDate end, Integer unit) {
        // 计算公共车间的部分[设定公共车间为id = 15]
        Integer workshopId = 15;

        // 得到车间的数量 = workExtent[不等于公共车间的数量]
        List<DateStatistics> workMag = workshopDao.queryByWorkId(null);
        Integer workExtent = workMag
                .stream()
                .filter(w -> !"公共".equals(w.getDate()))
                .collect(Collectors.toList())
                .size();

        RangeStatistics rangeStatistics = WidgetUtil.findDateByRange(range, begin, end.plusDays(-1), "%Y-%m-%d", "'%Y-%m-%d'");
        // 用电
        List<DateByStatistics> electricity = new ArrayList<>();
        // 所有能耗
        List<DateStatistics> allEnergy = overallEnergyDao.findConsumeWaterByUnit(rangeStatistics.getDatetime(), workshopId, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
        // 4：压缩空气
        List<DateStatistics> compress = allEnergy.stream().filter(d -> d.getCount() == 4L).collect(Collectors.toList());
        // 1：冷却水(空调热能)
        List<DateStatistics> air = allEnergy.stream().filter(d -> d.getCount() == 1L).collect(Collectors.toList());
        // 2：软化水(自来水)
        List<DateStatistics> softening = allEnergy
                .stream()
                .filter(d -> d.getCount() == 2L)
                .collect(Collectors.toList());
        // 默认单位：能耗
        if (unit == 0) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricity(rangeStatistics.getDatetime(), workshopId, null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

        }
        // 能耗价格：万元
        if (unit == 1) {
            // 用电
            List<DateByStatistics> daoTotalCost = deviceEnergyMapper.findConsumeElectricityPrice(rangeStatistics.getDatetime(), workshopId, null, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 查询所有单价(查找8为峰电、9为平电、10为谷电的单价)
            electricity = WidgetUtil.mergeByDateBy(daoTotalCost);

            // 4：压缩空气
            // 总压缩空气设备电费/总压缩空气(流量计m3)=压缩空气单价x压缩空气能耗m3=压缩空气费用
            List<DateByStatistics> daoTotalByCompress = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("4"))
                    .collect(Collectors.toList());
            // 得到压缩空气总用电
            List<DateStatistics> compressByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCompress);
            // 得到压缩空气总能耗
            List<DateStatistics> cooling = WidgetUtil.mergeByEnergyPrice(compress);
            compress.stream().forEach(d -> {
                if (setScheme == true) {
                    if (compressByElectricity.isEmpty()) {
                        compressByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (cooling.isEmpty() || cooling.get(0).getNumber() == 0.00) {
                        cooling.add(new DateStatistics(0L, "", 1.00));
                        cooling.get(0).setNumber(1.00);
                    }
                    Double unitPrice = compressByElectricity.stream().findFirst().get().getNumber() / cooling
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    Double unitPrice = 0.00;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 热能(冷却水)
            // 电费 -> 1：设备类型为空调热能(冷却水)的
            List<DateByStatistics> daoTotalByCooling = daoTotalCost
                    .stream()
                    .filter(d -> d.getHour().equals("1"))
                    .collect(Collectors.toList());
            // 得到冷却水总用电
            List<DateStatistics> coolingByElectricity = WidgetUtil.mergeByElectricityPrice(daoTotalByCooling);
            // 得到冷却水总能耗
            List<DateStatistics> coolingCount = WidgetUtil.mergeByEnergyPrice(air);
            air.stream().forEach(d -> {
                if (setScheme == true) {
                    if (coolingByElectricity.isEmpty()) {
                        coolingByElectricity.add(new DateStatistics(0L, "", 0.00));
                    }
                    if (coolingCount.isEmpty() || coolingCount.get(0).getNumber() == 0.00) {
                        coolingCount.add(new DateStatistics(0L, "", 1.00));
                        coolingCount.get(0).setNumber(1.00);
                    }
                    Double unitPrice = coolingByElectricity.stream().findFirst().get().getNumber() / coolingCount
                            .stream()
                            .findFirst()
                            .get()
                            .getNumber();
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
                if (setScheme == false) {
                    // 设定冷却水的单价为0.4
                    Double unitPrice = 0.40;
                    d.setNumber(new BigDecimal(d.getNumber() * unitPrice / 10000)
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });

            // 自来水(软化水) * 单价
            softening = overallEnergyDao.findConsumeWater(rangeStatistics.getDatetime(), workshopId, null, null, rangeStatistics.getBegin(), rangeStatistics.getEnd());
            // 电能(万元)
            electricity.stream().forEach(e -> {
                e.setNumber(new BigDecimal(e.getNumber() / 10000).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
            });
        }

        // 电能
        List<DateStatistics> el = WidgetUtil.mergeElementByNow(electricity
                                                                       .stream()
                                                                       .map(d -> new DateStatistics(d
                                                                                                            .getType()
                                                                                                            .longValue(), d.getDate(), d.getNumber()))
                                                                       .collect(Collectors.toList()));
        // 压缩空气
        List<DateStatistics> co = WidgetUtil.mergeElementByNow(compress);
        // 冷却水(空调热能)
        List<DateStatistics> cl = WidgetUtil.mergeElementByNow(air);
        // 软化水
        List<DateStatistics> so = WidgetUtil.mergeElementByNow(softening);

        List<DateStatistics> statistics = new ArrayList<>();
        if (!el.isEmpty()) {
            el.get(0).setDate("用电");
            el.get(0).setCount(7L);
            statistics.add(el.get(0));
        } else {
            statistics.add(new DateStatistics(7L, "用电", 0.00));
        }
        if (!co.isEmpty()) {
            co.get(0).setDate("压缩空气");
            co.get(0).setCount(4L);
            statistics.add(co.get(0));
        } else {
            statistics.add(new DateStatistics(4L, "压缩空气", 0.00));
        }
        if (!cl.isEmpty()) {
            cl.get(0).setDate("空调热能");
            cl.get(0).setCount(11L);
            statistics.add(cl.get(0));
        } else {
            statistics.add(new DateStatistics(11L, "空调热能", 0.00));
        }
        if (!so.isEmpty()) {
            so.get(0).setDate("软化水");
            so.get(0).setCount(2L);
            statistics.add(so.get(0));
        } else {
            statistics.add(new DateStatistics(2L, "软化水", 0.00));
        }

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        // 处理接入的数据进行累加公共部分的数据
        el.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        co.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        cl.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        so.stream().forEach(e -> {
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() / workExtent));
        });

        if (electricList.isEmpty()) {
            electricList.add(new DateStatistics(7L, "用电", 0.00));
        }
        if (compressedAirData.isEmpty()) {
            compressedAirData.add(new DateStatistics(4L, "压缩空气", 0.00));
        }
        if (waterData.isEmpty()) {
            waterData.add(new DateStatistics(2L, "软化水", 0.00));
        }
        if (thermalList.isEmpty()) {
            thermalList.add(new DateStatistics(1L, "空调热能", 0.00));
        }

        electricList.stream().forEach(e -> {
            if (el.isEmpty()) {
                el.add(new DateStatistics(50L, "", 0.00));
            }
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() + el.get(0).getNumber()));
        });
        compressedAirData.stream().forEach(e -> {
            if (co.isEmpty()) {
                co.add(new DateStatistics(50L, "", 0.00));
            }
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() + co.get(0).getNumber()));
        });
        waterData.stream().forEach(e -> {
            if (cl.isEmpty()) {
                cl.add(new DateStatistics(50L, "", 0.00));
            }
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() + cl.get(0).getNumber()));
        });
        thermalList.stream().forEach(e -> {
            if (so.isEmpty()) {
                so.add(new DateStatistics(50L, "", 0.00));
            }
            e.setNumber(WidgetUtil.setPriceByTwoDecimals(e.getNumber() + so.get(0).getNumber()));
        });

        return result;
    }

    @Override
    public Map<String, List<DateStatistics>> findConjunctByEnergy(Integer unit, Integer type, List<DateStatistics> dateStatistics) {

        Map<String, List<DateStatistics>> result = new LinkedHashMap<>();

        // 用量
        if (unit == 0) {

            if (!dateStatistics.isEmpty()) {
                // 用电的能耗累加
                List<DateStatistics> electricity = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                        .stream()
                                                                                        .filter(e -> e.getCount() == 7L || e.getCount() == 8L || e.getCount() == 9L || e.getCount() == 10L)
                                                                                        .collect(Collectors.toList()));
                // 压缩空气的能耗累加
                List<DateStatistics> compress = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                     .stream()
                                                                                     .filter(e -> e.getCount() == 4L)
                                                                                     .collect(Collectors.toList()));
                // 软化水的能耗累加
                List<DateStatistics> softening = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                      .stream()
                                                                                      .filter(e -> e.getCount() == 2L)
                                                                                      .collect(Collectors.toList()));
                // 空调热能-冷却水的能耗累加
                List<DateStatistics> air = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                .stream()
                                                                                .filter(e -> e.getCount() == 1L)
                                                                                .collect(Collectors.toList()));
                // 蒸汽的能耗累加
                List<DateStatistics> vapour = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                   .stream()
                                                                                   .filter(e -> e.getCount() == 6L)
                                                                                   .collect(Collectors.toList()));

                if (!electricity.isEmpty()) {
                    result.put("7", WidgetUtil.mergeElementByNow(dateStatistics
                                                                         .stream()
                                                                         .filter(e -> e.getCount() == 7L)
                                                                         .collect(Collectors.toList())));
                    result.put("8", WidgetUtil.mergeElementByNow(dateStatistics
                                                                         .stream()
                                                                         .filter(e -> e.getCount() == 8L)
                                                                         .collect(Collectors.toList())));
                    result.put("9", WidgetUtil.mergeElementByNow(dateStatistics
                                                                         .stream()
                                                                         .filter(e -> e.getCount() == 9L)
                                                                         .collect(Collectors.toList())));
                    result.put("10", WidgetUtil.mergeElementByNow(dateStatistics
                                                                          .stream()
                                                                          .filter(e -> e.getCount() == 10L)
                                                                          .collect(Collectors.toList())));
                }
                if (!compress.isEmpty()) {
                    result.put("4", compress);
                }
                if (!softening.isEmpty()) {
                    result.put("2", softening);
                }
                if (!air.isEmpty()) {
                    result.put("1", air);
                }
                if (!vapour.isEmpty()) {
                    result.put("6", vapour);
                }
            }

        }
        // 费用
        if (unit == 1) {

            // 默认的计算
            if (type == 1) {
                // 找到能源设置的单价
                List<DateStatistics> priceSetting = energyPriceSettingDao.findPriceByType();

                if (!dateStatistics.isEmpty()) {
                    // 7为尖电[spikes]，8为峰电[peaks]，9为平电[parity]，10为谷电[lowEbb]
                    List<DateStatistics> spikes = dateStatistics
                            .stream()
                            .filter(e -> e.getCount() == 7L)
                            .collect(Collectors.toList());
                    List<DateStatistics> peaks = dateStatistics
                            .stream()
                            .filter(e -> e.getCount() == 8L)
                            .collect(Collectors.toList());
                    List<DateStatistics> parity = dateStatistics
                            .stream()
                            .filter(e -> e.getCount() == 9L)
                            .collect(Collectors.toList());
                    List<DateStatistics> lowEbb = dateStatistics
                            .stream()
                            .filter(e -> e.getCount() == 10L)
                            .collect(Collectors.toList());

                    // 得到对应的单价
                    Double spikesPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 8L)
                            .findFirst()
                            .get()
                            .getNumber();
                    Double parityPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 9L)
                            .findFirst()
                            .get()
                            .getNumber();
                    Double lowEbbPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 10L)
                            .findFirst()
                            .get()
                            .getNumber();
                    Double compressPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 4L)
                            .findFirst()
                            .get()
                            .getNumber();
                    Double softeningPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 2L)
                            .findFirst()
                            .get()
                            .getNumber();
                    Double airPrice = priceSetting
                            .stream()
                            .filter(p -> p.getCount() == 1L)
                            .findFirst()
                            .get()
                            .getNumber();

                    List<DateStatistics> compress = WidgetUtil.setPriceByDateStatistics(compressPrice, WidgetUtil.mergeElementByNow(dateStatistics
                                                                                                                                            .stream()
                                                                                                                                            .filter(e -> e.getCount() == 4L)
                                                                                                                                            .collect(Collectors.toList())));
                    List<DateStatistics> softening = WidgetUtil.setPriceByDateStatistics(softeningPrice, WidgetUtil.mergeElementByNow(dateStatistics
                                                                                                                                              .stream()
                                                                                                                                              .filter(e -> e.getCount() == 2L)
                                                                                                                                              .collect(Collectors.toList())));
                    List<DateStatistics> air = WidgetUtil.setPriceByDateStatistics(airPrice, WidgetUtil.mergeElementByNow(dateStatistics
                                                                                                                                  .stream()
                                                                                                                                  .filter(e -> e.getCount() == 1L)
                                                                                                                                  .collect(Collectors.toList())));
                    List<DateStatistics> vapour = WidgetUtil.mergeElementByNow(dateStatistics
                                                                                       .stream()
                                                                                       .filter(e -> e.getCount() == 6L)
                                                                                       .collect(Collectors.toList()));
                    if (!dateStatistics.isEmpty()) {
                        result.put("7", WidgetUtil.setPriceByDateStatistics(spikesPrice * 1.25, spikes));
                        result.put("8", WidgetUtil.setPriceByDateStatistics(spikesPrice, peaks));
                        result.put("9", WidgetUtil.setPriceByDateStatistics(parityPrice, parity));
                        result.put("10", WidgetUtil.setPriceByDateStatistics(lowEbbPrice, lowEbb));
                    }
                    if (!compress.isEmpty()) {
                        result.put("4", compress);
                    }
                    if (!softening.isEmpty()) {
                        result.put("2", softening);
                    }
                    if (!air.isEmpty()) {
                        result.put("1", air);
                    }
                    if (!vapour.isEmpty()) {
                        List<DateStatistics> natural = purchasingEnergyDao.findNaturalByEndDate(vapour
                                                                                                        .stream()
                                                                                                        .findFirst()
                                                                                                        .get()
                                                                                                        .getDate());
                        natural.stream().forEach(n -> {
                            WidgetUtil.setPriceByTwoDecimals(n.getNumber() / 10000);
                        });
                        result.put("6", natural);
                    }
                }

            }
            // 当月的计算
            /*if (type == 2) {

            }*/
        }

        return result;
    }

}
